package cn.auction.dao.impl;

import cn.auction.dao.AuctionDao;
import cn.auction.entity.Auction;
import cn.auction.entity.AuctionRecord;
import cn.auction.entity.AuctionUser;
import cn.auction.util.Page;
import my.framework.dao.CommonDao;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AuctionDaoImpl implements AuctionDao {
    private CommonDao dao = new CommonDao();

    public AuctionDaoImpl() {
    }

    @Override
    public Page<Auction> selectByPage(Auction auction, int pageIndex) throws Exception {
       Page<Auction> page=new Page();
       List<Object> params=new ArrayList<>();
        String sqlAll=" from auction where 1=1 ";
       //当商品为名称不为空时
       if (auction.getAuctionName()!=null && !auction.getAuctionName().equals("")){
           sqlAll+=" and auctionName like ? ";
           params.add("%" + auction.getAuctionName() + "%");
       }
       //当描述不为空时
       if (auction.getAuctionDesc()!=null && !auction.getAuctionDesc().equals("")){
           sqlAll+=" and auctionDesc like ? ";
           params.add("%" + auction.getAuctionDesc() + "%");
       }
       //当开始时间不为空时
        if(auction.getAuctionStartTime()!=null){
            sqlAll+=" and auctionStartTime >= ? ";
            params.add(new Timestamp(auction.getAuctionStartTime().getTime()));
        }
        //当结束时间不为空
        if(auction.getAuctionEndTime()!=null){
            sqlAll+=" and auctionEndTime >= ? ";
            params.add(new Timestamp(auction.getAuctionEndTime().getTime()));
        }
        //当起拍价不为空时
        if (auction.getAuctionStartPrice()!=null){
            sqlAll+=" and auctionStartPrice = ? ";
            params.add(auction.getAuctionStartPrice());
        }
        int count=(Integer)this.dao.executeQuery(Integer.class,"select count(*)"+sqlAll,params.toArray()).get(0);
        page.setCount(count);
        page.setPageIndex(pageIndex);
        sqlAll+=" limit ?,? ";
        params.add((pageIndex - 1) * 10);
        params.add(10);
        List<Auction> list = this.dao.executeQuery(Auction.class, "select *"+sqlAll, params.toArray());
        page.setPageList(list);
        return page;
    }

    //添加拍卖信息
    public void insert(Auction auction) throws Exception {
        String sql = "insert into auction(auctionName, auctionStartPrice, auctionUpset, auctionStartTime, auctionEndTime, auctionPic, auctionPicType, auctionDesc) values(#{auctionName}, #{auctionStartPrice}, #{auctionUpset}, #{auctionStartTime}, #{auctionEndTime}, #{auctionPic}, #{auctionPicType}, #{auctionDesc})";
         //调用commdao的方法
        this.dao.executeUpdate(sql, auction);
    }
    //添加竞拍信息
    public void insert(AuctionRecord record) throws Exception {
        String sql = "insert into auctionRecord(userId, auctionId, auctionTime, auctionPrice) values(#{userId}, #{auctionId}, #{auctionTime}, #{auctionPrice})";
        this.dao.executeUpdate(sql, record);
    }
    //根据拍卖品编号查询拍卖品信息
    public Auction selectAuctionById(Long auctionId) throws Exception {
        String sql = "select * from auction where auctionId = ?";
        List<Auction> result = this.dao.executeQuery(Auction.class, sql, new Object[]{auctionId});
        return result != null && result.size() == 1 ? (Auction)result.get(0) : null;
    }
    //根据拍卖品编号查询竞拍信息，并按价格排序
    public List<AuctionRecord> selectAuctionRecordByAuctionId(Long auctionId) throws Exception {
        String sql = "select * from auctionRecord where auctionId = ? order by auctionPrice desc";
        return this.dao.executeQuery(AuctionRecord.class, sql, new Object[]{auctionId});
    }
    //内连接查询，查询拍卖品信息，当竞拍结束时间小于当前时间时
    public List<Auction> selectAuctionByTimeOver(AuctionUser user) throws Exception {
        String sql="select a.* from auction a inner join auctionrecord r on a.auctionId=r.auctionId where a.auctionEndTime < ? and r.userId= ? ";
        return this.dao.executeQuery(Auction.class, sql, new Object[]{new Timestamp((new Date()).getTime()), user.getUserId()});
    }
    //内连接查询，查询拍卖品信息，当竞拍结束时间大于当前时间时
    public List<Auction> selectAuctionByIntime(AuctionUser user) throws Exception {
        String sql = "select a.* from auction a inner join auctionrecord r on a.auctionId = r.auctionId where a.auctionEndTime > ? and r.userId = ?";
        return this.dao.executeQuery(Auction.class, sql, new Object[]{new Timestamp((new Date()).getTime()), user.getUserId()});
    }

}
